package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Comment;
import com.softeem.music_sys.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "评论接口", description = "评论相关操作：增删改查等")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    // 添加评论
    @Operation(summary = "添加评论", description = "添加评论，提供JSON格式评论数据")
    @PostMapping
    public Result add(@RequestBody Comment comment) {
        // 设置默认状态为正常
        comment.setStatus(1);
        // 设置默认点赞数为0
        comment.setLikeCount(0);
        boolean flag = commentService.save(comment);
        return flag ? Result.success().setMsg("评论成功").setData(comment) : Result.fail("评论失败");
    }

    // 删除评论
    @Operation(summary = "删除评论", description = "根据评论ID删除评论")
    @Parameters({
            @Parameter(name = "id", description = "评论ID")
    })
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        // 检查评论是否存在
        Comment comment = commentService.getById(id);
        if (comment == null) {
            return Result.fail("评论不存在");
        }
        boolean flag = commentService.removeById(id);
        return flag ? Result.success().setMsg("删除成功") : Result.fail("删除失败");
    }

    // 修改评论（主要用于修改点赞数）
    @Operation(summary = "修改评论", description = "修改评论，提供JSON格式评论数据")
    @PutMapping
    public Result update(@RequestBody Comment comment) {
        boolean flag = commentService.updateById(comment);
        return flag ? Result.success().setMsg("修改成功") : Result.fail("修改失败");
    }

    // 评论列表查询（分页）
    @Operation(summary = "评论列表查询", description = "评论列表查询，支持分页")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit
    ) {
        Page<Comment> pageUtil = new Page<>(page, limit);
        List<Comment> list = commentService.list(pageUtil);
        return Result.success().setData(list);
    }

    // 根据ID获取评论信息
    @Operation(summary = "获取评论信息", description = "根据评论ID获取评论详细信息")
    @Parameters({
            @Parameter(name = "id", description = "评论ID")
    })
    @GetMapping("/{id}")
    public Result getCommentById(@PathVariable Integer id) {
        Comment comment = commentService.getById(id);
        if (comment == null) {
            return Result.fail("评论不存在");
        }
        return Result.success().setData(comment);
    }

    // 根据评论对象类型和ID查询评论列表
    @Operation(summary = "根据对象查询评论", description = "根据评论对象类型和ID查询评论列表")
    @Parameters({
            @Parameter(name = "type", description = "评论对象类型"),
            @Parameter(name = "targetId", description = "评论对象ID")
    })
    @GetMapping("/byTarget")
    public Result getCommentByTarget(
            @RequestParam String type,
            @RequestParam Integer targetId
    ) {
        List<Comment> list = commentService.list(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Comment>()
                        .eq("type", type)
                        .eq("target_id", targetId)
                        .eq("status", 1)
                        .orderByDesc("comment_time")
        );
        return Result.success().setData(list);
    }

    // 根据用户ID查询评论列表
    @Operation(summary = "根据用户ID查询评论", description = "根据用户ID查询该用户的所有评论")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID")
    })
    @GetMapping("/byUser/{userId}")
    public Result getCommentByUserId(@PathVariable Integer userId) {
        List<Comment> list = commentService.list(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Comment>()
                        .eq("user_id", userId)
                        .eq("status", 1)
                        .orderByDesc("comment_time")
        );
        return Result.success().setData(list);
    }
}